package com.guigu.tms.aggregation.controller;

import com.guigu.tms.aggregation.service.IOrderClassifyOrderService;
import com.guigu.tms.aggregation.service.ITaskPickupDispatchService;
import com.guigu.tms.aggregation.service.ITruckDriverService;
import com.guigu.tms.commons.aggregation.entity.*;
import com.guigu.tms.commons.order.feign.OrderFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 司机表 前端控制器
 * </p>
 *
 * @author wj
 * @since 2022-10-21
 */
@RestController
@RequestMapping("/truckDriver")
public class TruckDriverController {

    @Autowired
    ITaskPickupDispatchService taskPickupDispatchService;

    @Autowired
    IOrderClassifyOrderService iOrderClassifyOrderService;

    @Autowired
    ITruckDriverService iTruckDriverService;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @PostMapping("/ShowTruckDriver")
    public List<DriverJob> ShowDriverJob(@RequestBody Map<String,String> map){
        return iTruckDriverService.ShowDriverJob(map);
    }

    @PostMapping("/UploadJf")
    public int UploadJf(@RequestParam String jobId,@RequestParam String taskId,@RequestParam String deliverPicture,@RequestParam String transportCertificate){
        return iTruckDriverService.UploadJf(jobId,taskId,deliverPicture,transportCertificate);
    }

    @PostMapping("/UploadTh")
    public int UploadTh(@RequestParam String jobId,@RequestParam String taskId,@RequestParam String cargoPickUpPicture){
        System.out.println("jobId-------"+jobId+",,,,,,"+"taskId-------"+taskId+",,,,,,"+"cargoPickUpPicture"+cargoPickUpPicture);
        return iTruckDriverService.UploadTh(jobId,taskId,cargoPickUpPicture);
    }


    @GetMapping("/CreateTaskPickupDispatch")
    //根据车次id(tripsId)去查询订单分类ID，拿到订单分类ID去查订单ID
    //拿到运输任务目的地机构ID 去判断订单分类中的目的地机构id是否一致 一样生成派件任务,否则不生成派件任务
    public int CreateTaskPickupDispatch(@RequestParam String tripsId,@RequestParam(value = "endId", required = true) String endId){
        System.out.println("endId-------------"+endId);
        int res=0;
        List<OrderClassifyOrder> orderClassifyOrders = iOrderClassifyOrderService.ShowOrderClassifyOrder(tripsId);
        for(int i=0;i<orderClassifyOrders.size();i++){
            //拿到订单分类id去查订单分类表中的id
            String orderClassifyId = orderClassifyOrders.get(i).getOrderClassifyId();
            System.out.println("订单分类ID-----"+orderClassifyId);
            //拿到orderId
            String orderId = orderClassifyOrders.get(i).getOrderId();
            System.out.println("订单Id---------"+orderId);
            OrderClassify orderClassify = iOrderClassifyOrderService.ShowOrderClassify(orderClassifyId);
            //拿到订单分类表中的目的地机构ID
            String endAgencyId = orderClassify.getEndAgencyId();
            //判断运输任务目的地机构id是否与订单分类表中的目的地机构id一致
            if(!endAgencyId.equals(endId)){
                System.out.println("目的地机构id不一致,无法创建派件任务");
                //目的地机构id不一致,无法创建派件任务
                res=2;
            }else{
                SimpleDateFormat simpleDateFormat;
                simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                Date date = new Date();
                String str = simpleDateFormat.format(date);
                Random random = new Random();
                int rannum = (int) (random.nextDouble() * (999 - 100 + 1)) + 100;
                String s=str+rannum;
                //创建派件任务
                TaskPickupDispatch taskPickupDispatch=new TaskPickupDispatch();
                //给TaskPickupDispatch对象赋值
                taskPickupDispatch.setId(s);
                taskPickupDispatch.setOrderId(orderId);
                taskPickupDispatch.setTaskType(2);
                taskPickupDispatch.setStatus(1);
                taskPickupDispatch.setAgencyId(endId);
                taskPickupDispatch.setEstimatedStartTime(LocalDateTime.now());
                taskPickupDispatch.setEstimatedEndTime(LocalDateTime.now().plusMinutes(30));
                taskPickupDispatch.setCancelTime(LocalDateTime.now());
                taskPickupDispatch.setCreateTime(LocalDateTime.now());
                // 将订单位置信息查出来
                OrderLocation orderLocation = iOrderClassifyOrderService.ShowOrderLocation(orderId);
                String position=orderLocation.getReceiveLocation();
                String courierId=orderFeignClient.getCourierId(position);
                taskPickupDispatch.setCourierId(courierId);
                // 判断courierId是否为空
                if(courierId==null){
                    taskPickupDispatch.setAssignedStatus(3);
                }else{
                    taskPickupDispatch.setAssignedStatus(2);
                }
                res=iOrderClassifyOrderService.AddTaskPickupDispatch(taskPickupDispatch);
            }
        }
        return res;
    }



}
